package com.shiyi.design.prototype;

import java.io.*;

/**
 * @author wangzhanshuai
 * @date 2021/1/25 9:49 下午
 * @since 1.0
 */
public class Sheep implements Cloneable, Serializable {

    private String name;

    private int age;

    private String color;

    private Sheep friend;

    public Sheep() {

    }

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Sheep getFriend() {
        return friend;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    @Override
    protected Sheep clone() {
        try {
            return (Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 深拷贝方式1
     * @return
     */
    protected Sheep deepClone1() {
        try {
            Sheep sheep = (Sheep) super.clone();
            if (this.getFriend() != null) {
                sheep.setFriend(this.getFriend().clone());
            }
            return sheep;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 深拷贝方式2--序列化
     * @return
     */
    protected Sheep deepClone2() {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            // 序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            Sheep sheep = (Sheep) super.clone();
            oos.writeObject(sheep);

            // 反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            sheep = (Sheep) ois.readObject();
            return sheep;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(bos != null) {
                    bos.close();
                }

                if(oos != null) {
                    oos.close();
                }

                if(bis != null) {
                    bis.close();
                }

                if(ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", friend=" + friend +
                '}';
    }
}
